<?php

session_start();

// include settings and functions
include "../conn/database.php";
include "functions.php";
include 'UUID.php';

// if the user submit the form
if(isset($_POST['submit'])) {
	// initialize errors values
	$errors=0;
	$errors_text = '';
	// cleanning the post data from hacking attempt
	$post_data = clean($_POST);
	
	// empty post data verification
	if(empty($post_data['send-addresse']) || empty($post_data['send-message'])) {
		$errors_text .= "Des champs sont vides<br />";
		$errors++;
	}
	
	
	// if no errors we proceed
	if($errors == 0) {
		$wid = $_SESSION['wid'];
		
		// data split
		$filter_emails = filter(preg_split("/[\s,;，；]+/",$post_data['send-addresse'],-1,PREG_SPLIT_NO_EMPTY));
		
		//string lower
		$lower_email = array();
		foreach($filter_emails as $filter_email){
			array_push($lower_email,strtolower($filter_email));
		}
		
		// Distinct
		$emails = array_unique($lower_email);
		
		//email vertification
		$error_emails;
		$right_emails;
		$will_send;
		foreach($emails as $email) {
			if(!check_email($email)) {
				$error_emails[] = $email;
			} else $right_emails[] = $email;
		}
		
		// check if the email already sent
		$already_sent = array();
		$already_sent_in_will_sent = array();
		$check_sql = "
		SELECT `EMAIL_ADDRESS` FROM `T_PENDING_INVITATION` WHERE `WEDDING_ID` = '".$wid."'";
		$res = mysql_query($check_sql);
		$rows = mysql_num_rows($res);
		if($rows>0){
			while($result = mysql_fetch_array($res)){
				array_push($already_sent,$result['EMAIL_ADDRESS']);
			}
		}
		if(!empty($right_emails))
			$will_send = $right_emails;
			$already_sent_in_will_sent = $right_emails;
		if(!empty($already_sent) && !empty($right_emails)) {
			$will_send = array_diff($right_emails,$already_sent);
		}
		if(!empty($will_send) && !empty($right_emails)) {
			$already_sent_in_will_sent = array_diff($right_emails,$will_send);
		}

		if(!empty($will_send)){
			foreach($will_send as $insert_email){
				$insert_sql = "
					INSERT INTO
					`T_PENDING_INVITATION`(`EMAIL_ADDRESS`, `IS_CONFIRMED`, `WEDDING_ID`, `IS_INVITATION_SENT`)
					VALUES
					('$insert_email',0,'$wid',1)"
				;
				smtp_mail_invite($email, "invite", "come to my wedding", "verification@promessedunevie.com", "Promesse d'une Vie");
				if(mysql_query($insert_sql)){
				} else {
					$errors_text .= mysql_error()."<br />";
					$errors++;
				}
			}
		}
	}
	if($errors == 0) {
		$error_emails_str = '';
		if(!empty($error_emails)){
			foreach($error_emails as $email) {
				$error_emails_str .= $email."; ";
			}
		}
		$already_sent_str = '';
		if(!empty($already_sent_in_will_sent)){
			foreach($already_sent_in_will_sent as $email) {
				$already_sent_str .= $email."; ";
			}
		}
		$will_send_str = '';
		if(!empty($will_send)){
			foreach($will_send as $email) {
				$will_send_str .= $email."; ";
			}
		}
		$arr['success'] = "1"; 
        $arr['error_emails'] = "Wrong emails: $error_emails_str";
        $arr['already_sent'] = "The emails you already sent before: $already_sent_str";
        $arr['will_send'] = "The emails send success: $will_send_str";
		$arr['msg'] = "Wrong emails: $error_emails_str <br >The emails you already sent before: $already_sent_str <br >The emails send success: $will_send_str";
	} else {
		$arr['success'] = "0"; 
        $arr['msg'] = $errors_text;
	}
	echo json_encode($arr);
}
?>